package com.tredreams.idea.counter;


import com.example.idea.counter.R;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

	public TextView count;
	public ListView lv;
	public Integer ideaCount = 0;
	private Accountdatabase ad;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ad = new Accountdatabase(this);
        Button add = (Button) findViewById(R.id.idea_button);
        final EditText ideaNote = (EditText) findViewById(R.id.ideaNote);
         lv = (ListView) findViewById(android.R.id.list);
        count = (TextView) findViewById(R.id.textView1);
        /*register listview to context menu to display the menu option on longclick*/
        registerForContextMenu(lv);
        fillData();
        /* insert the idea to database when user clicks on the idea button */
        add.setOnClickListener(new View.OnClickListener() {	
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(ideaNote.getText().toString().equals("")){
					warning();
				}else{
				ad.insert(ideaNote.getText().toString());
				Log.v("String is inserted", ideaNote.getText().toString());
				fillData();
				ideaNote.getText().clear();
				}
			}			
		});
        
        lv.setOnLongClickListener(new OnLongClickListener() {
			
			public boolean onLongClick(View v) {
				// TODO Auto-generated method stub
				v.showContextMenu();
				Log.v("click event is found", "find the long clink listener");
				return true;
			}
		});
        
    }  

    @Override
    public boolean onContextItemSelected(MenuItem item) {
      AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
      
      	@SuppressWarnings("rawtypes")
		ArrayAdapter adapter = (ArrayAdapter) lv.getAdapter();
      	String name = (String) adapter.getItem(info.position);
      	Log.v("iteam selected is", name);
      	switch (item.getItemId()) {
      	case R.id.delete:
      		/*ad = new Accountdatabase(this);*/
      		ad.deleteIdeaByName(name);
      		fillData();
      		/*ad.close();*/
      		return true;
      	default:
      		return super.onContextItemSelected(item);
      }
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
                                    ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    /* Reading the data from database*/
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private void fillData() {
        // Get all of the notes from the database and create the item list
		ArrayAdapter adp = new ArrayAdapter(this, R.layout.idealistview, ad.ideaList());
 	    lv.setAdapter(adp);
 	    ideaCount = ad.findNumberOfIdeas();
 	    count.setText(String.valueOf(ideaCount));
    }
	
	protected void warning() {
		// TODO Auto-generated method stub
		Toast toast = Toast.makeText(this, R.string.warning, Toast.LENGTH_SHORT);
		toast.setGravity(Gravity.TOP, 0, 40);
		toast.show();
	}

}
